package cn.mily.basics.controller;


import cn.mily.basics.utils.FastDfsApiOpr;
import cn.mily.basics.utils.JsonResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    /*上传logo图片*/
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile file) {
        /*获取文件后缀*/
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        try {
            /*向文件服务器上传文件*/
            String url = FastDfsApiOpr.upload(file.getBytes(), extension);
            JsonResult jsonResult = new JsonResult();
            /*设置路径*/
            jsonResult.setFileUrl(url);
            return jsonResult;
        } catch (IOException e) {
            e.printStackTrace();
            return new JsonResult();
        }
    }
}
